package com.fjrz.bbxwj.mode.api

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.fjrz.bbxwj.app.TOKEN
import com.fjrz.bbxwj.app.USER_INFO_ME
import com.fjrz.bbxwj.entity.UserLoginResponse
import com.fjrz.bbxwj.entity.UserMeResponse
import com.fjrz.bbxwj.entity.UserSmsResponse
import com.fjrz.bbxwj.entity.VideoDraftResponse
import com.module.lemlin.mode.SubscribeListener
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.subscribe
import com.fjrz.bbxwj.entity.*
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import rxhttp.RxHttp


/**
 * 发送短信验证码
 *
 * @param mobile 手机号
 * @param event 事件：login,set_pwd
 * */
fun userSmsSend(
    mobile: String, event: String, listener: SubscribeListener<UserSmsResponse>
): Disposable = subscribe(
    RxHttp.postForm("/api/sms/send")
        .setAssemblyEnabled(false)
        .add("mobile", mobile)
        .add("event", event)
        .asClass(UserSmsResponse::class.java), listener
)

/**
 * 短信验证码登录
 *
 * @param mobile 手机号
 * @param smsCode 短信验证码
 * */
fun userLogin(
    mobile: String, smsCode: String, listener: SubscribeListener<UserMeResponse>
): Disposable = subscribe(
    RxHttp.postForm("/api/user/login")
        .setAssemblyEnabled(false)
        .add("mobile", mobile)
        .add("sms_code", smsCode)
        .asClass(UserLoginResponse::class.java)
        .map {
            it.apply { SPStaticUtils.put(TOKEN, it.data.token) }
        }
        .flatMap { user() }, listener
)

/**
 * 个人信息
 * */
fun userMe(listener: SubscribeListener<UserMeResponse>): Disposable =
    subscribe(
        user(), listener
    )

fun user(): Observable<UserMeResponse> = RxHttp.postForm("/api/user/me")
    .asClass(UserMeResponse::class.java)
    .map { it.apply { SPStaticUtils.put(USER_INFO_ME, GsonUtils.toJson(data)) } }

/**
 * 用户信息
 *
 * @param userId
 * */
fun userInfo(
    userId: String, listener: SubscribeListener<UserInfoResponse>
) = subscribe(
    RxHttp.get("/api/user/info")
        .add("user_id", userId)
        .asClass(UserInfoResponse::class.java), listener
)

/**
 * 关注
 *
 * @param userId
 * */
fun userFollow(
    userId: String, listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/follow")
        .add("user_id", userId)
        .asClass(HttpServiceData::class.java), listener
)

/**
 * 取消关注
 *
 * @param userId
 * */
fun userUnFollow(
    userId: String, listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/unfollow")
        .add("user_id", userId)
        .asClass(HttpServiceData::class.java), listener
)

/**
 * 关注列表
 *
 * @param page 页数
 * @param limit 条数
 * */
fun userFollowList(
    page: String, limit: String, listener: SubscribeListener<UserAttentionResponse>
) = subscribe(
    RxHttp.get("/api/user/follow_list")
        .add("page", page)
        .add("limit", limit)
        .asClass(UserAttentionResponse::class.java), listener
)

/**
 * 粉丝列表
 *
 * @param page 页数
 * @param limit 条数
 * */
fun userFansList(
    page: String, limit: String, listener: SubscribeListener<UserAttentionResponse>
) = subscribe(
    RxHttp.get("/api/user/fans_list")
        .add("page", page)
        .add("limit", limit)
        .asClass(UserAttentionResponse::class.java), listener
)

/**
 * 互相关注列表
 *
 * @param page 页数
 * @param limit 条数
 * */
fun userMutualFollowList(
    page: String, limit: String, listener: SubscribeListener<UserAttentionResponse>
) = subscribe(
    RxHttp.get("/api/user/mutual_follow_list")
        .add("page", page)
        .add("limit", limit)
        .asClass(UserAttentionResponse::class.java), listener
)

/**
 * 作品列表
 *
 * @param userId
 * @param page 页数
 * @param limit 条数
 * */
fun userVideos(
    userId: String, page: String, limit: String, listener: SubscribeListener<VideoWorksResponse>
) = subscribe(
    RxHttp.get("/api/user/videos")
        .add("user_id", userId)
        .add("page", page)
        .add("limit", limit)
        .asClass(VideoWorksResponse::class.java), listener
)

/**
 * 喜欢的作品列表
 *
 * @param userId
 * @param page 页数
 * @param limit 条数
 * */
fun userLikeVideos(
    userId: String, page: String, limit: String, listener: SubscribeListener<VideoLikesResponse>
) = subscribe(
    RxHttp.get("/api/user/like_videos")
        .add("user_id", userId)
        .add("page", page)
        .add("limit", limit)
        .asClass(VideoLikesResponse::class.java), listener
)

/**
 * 我的作品 / 草稿箱
 *
 * @param status 状态 0草稿箱,1待审核,2审核通过,3:审核拒绝
 * @param page 页数
 * @param limit 条数
 * */
fun userMyVideos(
    status: String, page: String, limit: String, listener: SubscribeListener<VideoDraftResponse>
) = subscribe(
    RxHttp.get("/api/user/my_videos")
        .add("status", status)
        .add("page", page)
        .add("limit", limit)
        .asClass(VideoDraftResponse::class.java), listener
)

/**
 * 设置访问密码
 *
 * @param smsCode 短信验证码
 * @param password 密码
 * @param confirmPassword 确认密码
 * */
fun userSetPwd(
    smsCode: String,
    password: String,
    confirmPassword: String,
    listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/set_pwd")
        .add("sms_code", smsCode)
        .add("password", password)
        .add("confirm_password", confirmPassword)
        .asClass(HttpServiceData::class.java), listener
)

/**
 * 私人草稿箱视频
 *
 * @param password 访问密码
 * @param page 页数
 * @param limit 条数
 * */
fun userPrivateDraftVideos(
    password: String, page: String, limit: String, listener: SubscribeListener<VideoDraftResponse>
) = subscribe(
    RxHttp.get("/api/user/private_draft_videos")
        .add("password", password)
        .add("page", page)
        .add("limit", limit)
        .asClass(VideoDraftResponse::class.java), listener
)

/**
 * 修改用户信息
 *
 * @param userRequest
 * */
fun userProfile(
    userRequest: UserRequest,
    listener: SubscribeListener<HttpServiceData>
) = subscribe(
    Observable.just(userRequest).flatMap {
        val maps: Map<String, String?> =
            GsonUtils.fromJson<Map<String, String?>>(
                GsonUtils.toJson(userRequest),
                Map::class.java
            ).filter { it.value != null }
        Observable.just(maps)
    }.flatMap {
        RxHttp.postForm("/api/user/profile")
            .addAll(it)
            .asClass(HttpServiceData::class.java)
    }, listener
)

/**
 * 获取数据总览
 * */
fun userStat(
    listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/stat")
        .asClass(HttpServiceData::class.java), listener
)

/**
 * 屏蔽视频 / 用户
 *
 * @param type 类型：1=屏蔽视频 2=屏蔽用户
 * @param targetId 视频ID / 用户ID
 * */
fun userShield(
    type: String, targetId: String, listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/shield")
        .add("type", type)
        .add("target_id", targetId)
        .asClass(HttpServiceData::class.java), listener
)

/**
 * 实名认证
 *
 * @param name 姓名
 * @param identityNumber 身份证号
 * @param idCardFrontImage 身份证正面照
 * @param idCardBackImage 身份证背面照
 * */
fun userCertificate(
    name: String,
    identityNumber: String,
    idCardFrontImage: String,
    idCardBackImage: String,
    listener: SubscribeListener<HttpServiceData>
) = subscribe(
    RxHttp.postForm("/api/user/certificate")
        .add("name", name)
        .add("identity_number", identityNumber)
        .add("id_card_front_image", idCardFrontImage)
        .add("id_card_back_image", idCardBackImage)
        .asClass(HttpServiceData::class.java), listener
)
